<template>
  <div v-show="option === 'register'" class="container">
    <el-form ref="formRef" :model="form" :rules="rules" class="form-bar" label-width="120px">
      <el-form-item prop="username">
        <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="Avatar"
            size="large"
        />
      </el-form-item>
      <el-form-item prop="nickname">
        <el-input
            v-model="form.nickname"
            placeholder="请输入昵称"
            prefix-icon="Avatar"
            size="large"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            size="large"
            type="password"
        />
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input
            v-model="form.confirmPassword"
            placeholder="请再次输入密码"
            prefix-icon="Lock"
            show-password
            size="large"
            type="password"
            @keyup.enter="handleRegister()"
        />
      </el-form-item>
      <el-form-item>
        <el-button
            :loading="loading"
            class="full-bar"
            size="large"
            type="primary"
            @click="handleRegister()"
        >注册
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button
            class="full-bar"
            size="large"
            @click="option = 'login'"
        >已有账号，返回登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script name="LoginRegister" setup>
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import {getUserInfo, login, register} from "@/api/login";
import useUserInfoStore from "@/stores/userInfo";

const {proxy} = getCurrentInstance();

const option = defineModel()
const loading = ref(false);
const formRef = ref()

const data = reactive({
  form: {
    username: null,
    nickname: null,
    password: null,
    confirmPassword: null,
  },
  rules: {
    username: [
      {required: true, message: "用户名不能为空", trigger: ["blur", "change"]}
    ],
    nickname: [
      {required: true, message: "昵称不能为空", trigger: ["blur", "change"]}
    ],
    password: [
      {required: true, message: "密码不能为空", trigger: ["blur", "change"]}
    ],
    confirmPassword: [
      {required: true, message: "确认密码不能为空", trigger: ["blur", "change"]}
    ],
  }
});

const {form, rules} = toRefs(data);

function handleRegister() {
  formRef.value.validate((valid) => {
    if (valid) {
      if (form.value.password !== form.value.confirmPassword) {
        proxy.$modal.msgError('两次密码不一致，请重新输入')
        return
      }
      loading.value = true
      register(form.value).then(res => {
        if (res.data) {
          proxy.$modal.loading('正在加载系统中...')
          useUserInfoStore().setAccessToken(res.data.accessToken)
          useUserInfoStore().setRefreshToken(res.data.refreshToken)
          getUserInfo().then(res => {
            if (res.data) {
              useUserInfoStore().setUser(res.data)
            }
          })
        }
      }).finally(() => {
        loading.value = false
        proxy.$modal.closeLoading()
        proxy.$router.push('/')
      })
    }
  })
}
</script>

<style lang="scss" scoped>
.container {
  height: calc(100% - 60px);
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;

  .form-bar {
    width: 500px;
  }
}
</style>